home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Code Resources / Jims CDEFs 1.50 / demo Source ƒ / utilities / movableModal.c < prev    next >
Encoding:
Text File  |  1995-10-27  |  4.5 KB  |  207 lines  |  [TEXT/KAHL]

  1. //----------------------------------------------------------------------------------
  2. //    File        : movableModal.c
  3. //    Date        : April 4, 1994
  4. //    Author        : Jim Stout
  5. //    Purpose        : Implements movableModal dialogs
  6. //------------------------------------------------------------------------------------
  7. #include <Palettes.h>
  8. #include "movableModal.h"
  9.  
  10. #define CANCEL_ITEM_IS_2 1    // if dialog item 2 is "Cancel" button
  11.  
  12. // following must be in your application code!!
  13. // or remove the calls from this code...
  14.  
  15. extern void doUpdate        (EventRecord * theEvent);
  16. extern void doPeriodicEvent    (EventRecord * theEvent);
  17. extern void doMenuCommand    (long menuResult);
  18.  
  19. pascal void movableModalDialog(ModalFilterUPP filter, short *theItem)
  20. {
  21.     EventRecord theEvent;
  22.     DialogPtr    d,thisDialog;
  23.     GrafPtr        oldPort;
  24.     
  25.     thisDialog = FrontWindow();
  26.     GetPort(&oldPort);
  27.     SetPort(thisDialog);
  28.     
  29.     for(;;) {
  30.         WaitNextEvent(everyEvent, &theEvent, 20, 0L);
  31.         if( (*theItem = preFilterEvent(thisDialog, &theEvent)) != 0)
  32.             break;
  33.         
  34.         if (filter != nil)
  35.             if (CallModalFilterProc(filter, thisDialog, &theEvent, theItem))
  36.                 break;
  37.  
  38.         if (IsDialogEvent(&theEvent))
  39.             if (DialogSelect(&theEvent, &d, theItem))
  40.                 break;
  41.         
  42.         doPeriodicEvent(&theEvent);
  43.     }
  44.     SetPort(oldPort);
  45. }
  46.  
  47. static short preFilterEvent(DialogPtr d, EventRecord *theEvent)
  48. {
  49.     char        key;
  50.     short         ret=0,defItem,t;
  51.     long        ticks;
  52.     Handle        h;
  53.     Rect        r;
  54.     PenState    ps;
  55.     GDHandle    hThisDevice=0;
  56.     RgnHandle    c;
  57.     RGBColor    saveFore, saveBack={-1,-1,-1}, rgbGray={0,0,0};
  58.     GrafPtr        savePort;
  59.     
  60.     switch (theEvent->what) {
  61.         case mouseDown:
  62.             ret = doMouseDialog(d,theEvent);     /* handle drag etc. of dialog        */
  63.         break;
  64.         case diskEvt:
  65.             diskEvent(theEvent);
  66.         break;
  67.         case updateEvt:
  68.             if(d != (DialogPtr)theEvent->message)
  69.                 doUpdate(theEvent);
  70.             else {
  71.                 GetPort(&savePort);
  72.                 SetPort(d);
  73.                 
  74.                 BeginUpdate(d);
  75.                 EraseRect(&d->portRect);
  76.                 UpdtDialog(d, d->visRgn);
  77. //                DrawControls(d);
  78.                 EndUpdate(d);
  79.                 
  80.                 defItem = ((DialogPeek)d)->aDefItem;
  81.                 if(defItem) {
  82.                     c = NewRgn();
  83.                     GetClip(c);
  84.                     GetDItem(d, defItem, &t, &h, &r);
  85.                     if((**(ControlHandle)h).contrlHilite == 0xFF) {
  86.                         if(hasGrayText()) {
  87.                             GetForeColor(&saveFore);
  88.                             hThisDevice = GetGDevice();
  89.                             if(GetGray(hThisDevice, &saveBack, &rgbGray))
  90.                                 RGBForeColor(&rgbGray);
  91.                         }            
  92.                     }
  93.                     GetPenState(&ps);
  94.                     PenSize(3,3);
  95.                     PenPat(&qd.black);
  96.                     InsetRect(&r, -4, -4);
  97.                     ClipRect(&r);
  98.                     FrameRoundRect(&r, 16, 16);
  99.                     SetPenState(&ps);
  100.                     if(hThisDevice)
  101.                         RGBForeColor(&saveFore);
  102.                     SetClip(c);        
  103.                     DisposeRgn(c);
  104.                 }
  105.                 SetPort(savePort);
  106.             }
  107.         break;
  108.         
  109.         case keyDown:
  110.         case autoKey:
  111.             defItem = ((DialogPeek)d)->aDefItem;
  112.             if(!defItem)
  113.                 break;
  114.             key = (theEvent->message & charCodeMask);
  115.     
  116.              if (key == _RETURNKEY || key == _ENTRKEY){
  117.                 GetDItem(d, defItem, &t, &h, &r);
  118.                 if((**(ControlHandle)h).contrlHilite != 0xFF)
  119.                      ret = defItem;
  120.             }
  121. #if CANCEL_ITEM_IS_2
  122.             else
  123.              if (key == _ESCAPEKEY || (key == _PERIODKEY && 
  124.                      theEvent->modifiers & cmdKey) ) {
  125.                 GetDItem(d, 2, &t, &h, &r);
  126.                  ret = 2;
  127.             }
  128. #endif
  129.             if(h && ret) {
  130.                 HiliteControl((ControlHandle)h, inButton);
  131.                 Delay(8,&ticks);
  132.                 HiliteControl((ControlHandle)h, 0);
  133.             }
  134.         break;
  135.     }
  136.     return (ret);
  137. }
  138.  
  139. static short doMouseDialog(DialogPtr d, EventRecord *theEvent)
  140. {
  141.     WindowPtr    theWindow;
  142.     short        partCode, ret=0;
  143.     
  144.     switch (partCode = FindWindow(theEvent->where,&theWindow)) {
  145.         case inDrag:
  146.             if(theWindow == d) {
  147.                 DragWindow(d, theEvent->where, &qd.screenBits.bounds);
  148.                 theEvent->what = nullEvent;
  149.             }
  150.         break;
  151.         
  152.         case inMenuBar:
  153.             doMenuCommand(MenuSelect(theEvent->where));
  154.         break;
  155.  
  156.         case inGoAway:
  157.             if (TrackBox (theWindow, theEvent->where, partCode)) {
  158.                 ret = cancel;
  159.                 theEvent->what = nullEvent;
  160.             }
  161.         break;
  162.         
  163. /* add code if you need to deal with these mouseDown events…    */
  164.  
  165.         case inGrow:
  166.         break;
  167.         case inZoomIn:
  168.         case inZoomOut:
  169.         break;
  170.         case inContent:
  171.             if(theWindow != d) {
  172.                 SysBeep(1);
  173.             }
  174.         break;
  175.         default:
  176.         break;
  177.     }
  178.     return(ret);
  179. }
  180.  
  181. extern void diskEvent(EventRecord *theEvent)
  182. {
  183.     Point diskInitPt;
  184.  
  185.     if (HiWord (theEvent->message) != noErr) {
  186.         diskInitPt.v = 120;
  187.         diskInitPt.h = 100;
  188.         DILoad ();
  189.         (void) DIBadMount (diskInitPt, theEvent->message);
  190.         DIUnload ();
  191.         theEvent->what = nullEvent;
  192.     }
  193. }
  194. static Boolean hasGrayText    (void)
  195. {
  196.     OSErr        err;
  197.     short        bit = gestaltHasGrayishTextOr;
  198.     long        gResult;
  199.     Boolean        result = false;
  200.     
  201.     err = Gestalt(gestaltQuickdrawFeatures,&gResult);
  202.     if(err == noErr) {
  203.         if(BitTst(&gResult, 31-bit))
  204.             result = true;
  205.     }
  206.     return(result);
  207. }